<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="名称" prop="name">
        <el-input maxlength="100" v-model="queryParams.name" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="menu"><el-button @click="handleAdd" type="primary" icon="el-icon-circle-plus-outline"
        size="mini">新增</el-button></div>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="编号" width="180">
      </el-table-column>
      <el-table-column prop="name" label="名称" width="180">
      </el-table-column>
      <el-table-column prop="remark" label="备注">
      </el-table-column>
      <el-table-column prop="createTime" label="添加时间">
      </el-table-column>
      <el-table-column prop="createBy" label="创建人">
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <a @click="handleUpdate(scope.row)" style="color:#1890FF;">编辑</a>
          <a @click="handleDelete(scope.row)" style="color:red; margin-left: 15px;">删除</a>
          <a @click="linkArticleAction(scope.row)" style="color:red; margin-left: 15px;">添加文章</a>
          <a @click="shareAction(scope.row)" style="color:red; margin-left: 15px;">分享链接</a>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="租户名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入租户名称" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" placeholder="请输入备注" />
          <!-- <el-select v-model="form.wenzhangids" multiple filterable remote reserve-keyword placeholder="请输入关键词"
            :remote-method="remoteMethod" :loading="loading" style="width:100%;">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="'添加文章'" :visible.sync="linkDialog" width="50%" :close-on-click-modal="false" append-to-body>
      <linkArticle :uid="linkUid" @close="linkDialog = false"></linkArticle>
    </el-dialog>
  </div>
</template>

<script>
import linkArticle from './linkArticle.vue'
import { tenant_queryPage, tenant_delete, tenant_insert, tenant_update } from "@/api/tenant"

export default {
  name: 'Index',
  components: {
    linkArticle,
  },
  data() {
    return {
      title: '',
      dialogVisible: false,

      form: {
        name: '',
        remark: '',
      },

      queryParams: {
        name: '',
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      // 总条数
      total: 0,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "租户名称不能为空", trigger: "blur" }
        ],
      },

      options: [],
      value: [],
      list: [],
      loading: false,

      //关联文章
      linkUid:null,
      linkDialog:false,
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      tenant_queryPage(this.queryParams).then(res => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      this.form = row;
      this.open = true;
      this.title = "修改租户";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            tenant_update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            tenant_insert(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function () {
        return tenant_delete(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    //关联文章
    linkArticleAction(row){
      this.linkUid = row.id;
      this.linkDialog = true;
    },
    shareAction(item) {
      let data = location.origin + '/share?uid=' + item.id;
      console.log(data);
      window.open(data);
    },
    remoteMethod(query) {
      if (query !== '') {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;
          this.options = this.list.filter(item => {
            return item.label.toLowerCase()
              .indexOf(query.toLowerCase()) > -1;
          });
        }, 200);
      } else {
        this.options = [];
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.menu {
  display: flex;
  justify-content: end;
  margin-bottom: 15px;
}

.item {
  cursor: pointer;
  font-size: 16px;
  padding: 16px 24px 12px 24px;
  border-bottom: 1px solid #f0f2f5;

  .title {
    font-weight: bold;
  }

  .jianjie {
    color: #555666;
    margin-top: 8px;
  }

  .desc {
    margin-top: 8px;
    display: flex;
    align-items: center;

    .biaoqian {
      color: #fc5531;
      background-color: #fff5f2;
      padding: 3px 8px;
    }

    .zuozhe {
      margin-left: 15px;
    }
  }
}
</style>
